package com.augurit.wf.app.document.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil{
  
	//过期为true
	public static boolean isOutDate(Date another){
		Date now =new Date();
		if(now.getTime()<another.getTime()+86400000)
			return false;
		return true;
		
	}
	/**
	 * 取上期日期
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date SQ(String year,String month){
		if(month==null || month.equals("")){
			Date date = parseDate(year,"yyyy");
			Calendar ca = Calendar.getInstance();  
			ca.setTime(date);  
			ca.add(Calendar.YEAR, -1);  
			Date preYear = ca.getTime();
			return preYear;
		}else{
			Date date = parseDate(year+"-"+month,"yyyy-MM");
			Calendar ca = Calendar.getInstance();  
			ca.setTime(date);  
			ca.add(Calendar.MONTH, -1);  
			Date preYear = ca.getTime();
			return preYear;
		}
	}
	/**
	 * 获取同期日期
	 * @param year
	 * @return
	 */
	public static Date TQ(String year){
		Date date = parseDate(year,"yyyy");
		Calendar ca = Calendar.getInstance();  
		ca.setTime(date);  
		ca.add(Calendar.YEAR, -1);  
		Date preYear = ca.getTime();
		return preYear;
	}
	/**
	 * 解析日期
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date parseDate(String dateStr,String format){     
		  java.util.Date   date   =   null;     
		  try {     
			  java.text.DateFormat   df   =   new   java.text.SimpleDateFormat(format);    
			  date   =   (java.util.Date) df.parse(dateStr);     
		  }catch(Exception   e){     
			  e.printStackTrace();
		  }     
		  return   date;     
	}
	/**
	 * 获取年
	 * @param date
	 * @return
	 */
	public static String getYear(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy");  
        return sf.format(date);
	}
	/**
	 * 获取月份
	 * @param date
	 * @return
	 */
	public static String getMonth(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("MM");  
        return sf.format(date);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Date date = DateUtil.SQ("2009", null);
		Date date1 = DateUtil.SQ("2009", "02");
		Date date2 = DateUtil.TQ("2009");
		
		System.out.println(DateUtil.getYear(date));
		System.out.println(DateUtil.getYear(date1)+"-"+DateUtil.getMonth(date1));
		System.out.println(DateUtil.getYear(date2));
	}
}
